# -*- coding: utf-8 -*-
class basesearch:
  def __init__(self,logger,host,port="80",path="/",timeout=1.5):
    self.host=host
    self.port=port
    self.path=path
    self.timeout=timeout
    self.logger=logger
    self.parameters={}#Config parameters
    self.requests={}#Config parameters
    self.map_requests={}#Config map_requests
    self.env_requests={}#Env request
    self.error=True
    self.msg=""
    self.result=None
  def setParameters(self,d):
      self.parameters=d
  def setRequests(self,d):
      self.requests=d
  def setMapRequests(self,d):
      self.map_requests=d
  def setEnvRequests(self,d):
      self.env_requests=d
  def getUrl(self,envrequests):
    self.env_requests=envrequests
    self._makeUrl()
    return self.url
  
  def setError(self,msg):
    self.logger.debug("SetError for source %s - %s"%(__name__,msg))
    self.error=True
    self.msg=msg
  def getError(self):
    #Pioreturn self.msg if self.error else None
    if not self.error:
	return None
    else:
	return self.msg
  def clear(self):
    self.logger.debug("clear - %s"%(__name__))
    self.error=False
    self.result=None
  def __str__(self):
    s="%s"%__name__
    s+="".join(self.map_requests.keys())
    return s
   
  def _makeSourceRequest(self):
    r=""
    tmp_env_requests=None
    for mr in self.map_requests:
        if self.env_requests.has_key(mr):
          self.env_requests[self.map_requests[mr]]=self.env_requests[mr]
    for k in self.requests:
        if self.requests[k]['override']=='true':
          if k in self.env_requests:
              self.logger.debug("%s override %s from %s to %s"%(__name__,k,self.requests[k]['value'],self.env_requests[k]))
              r+="%s=%s&"%(k,self.env_requests[k])    
          else:
            r+="%s=%s&"%(k,self.requests[k]['value'])    
        else:
          r+="%s=%s&"%(k,self.requests[k]['value'])
    return r[0:(len(r)-1)]
    
  
